﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMSModel;
using System.Text;
using System.Data;
using DAL;

/// <summary>
///NewsImportBLL 的摘要说明
/// </summary>
public class NewsImportBLL
{

    public void ImportAll()
    {

    }

    /// <summary>
    /// 导入相关股票
    /// </summary>
    /// <param name="newsId">新闻编号</param>
    public void ImprtStock(long newsId, long localNewsId)
    {

        StockInfoDAL s = new StockInfoDAL();
        DataTable tb = s.GetStocksByNewsId(newsId);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string str = string.Empty;
        List<string> tempStocks = new List<string>();
        foreach (DataRow row in tb.Rows)
        {

            NewsRelatedStocks c = new NewsRelatedStocks();
            c.NewsId = localNewsId;
            c.Stock = Tools.GetString(row["ob_seccode_0026"]);
            if (tempStocks.Contains(c.Stock))
            {
                continue;
            }
            else
            {
                tempStocks.Add(c.Stock);
            }

            if (!string.IsNullOrEmpty(c.Stock) && c.Stock.Length == 6)
                str += c.Stock + ",";
            else
                continue;
            c.StockName = Tools.GetString(row["ob_secname_0026"]);
            if (dal.NewsRelatedStocks.Any(x => x.NewsId == localNewsId && x.Stock == c.Stock))
            {
                continue;
            }
            dal.NewsRelatedStocks.AddObject(c);
        }
        if (str.Length > 0)
        {
            str = str.Substring(0, str.Length - 1);
            try
            {
                var news = dal.News.First(x => x.id == localNewsId);
                news.Stock = str;
            }
            catch
            {
            }
        }
        dal.SaveChanges();
    }

    public void Import(long newId, string typeId)
    {
        StockInfoDAL dal = new StockInfoDAL();
        CMSModel.CMSEntities model = DAL.DBManager.GetCmsEmtity();
        long newsId = 0;
        if (!model.NewsImportId.Any(x => x.CpId == newId))
        {
            DataSet ds = dal.GetNewsById(newId);
            News news = new News();
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow row = ds.Tables[0].Rows[0];
                news.author = Tools.GetString(row["f001v_0021"]);
                StringBuilder sb = new StringBuilder(Tools.GetString(row["f006l_0021"]));
                sb = ReplaceKG(sb);
                sb = sb.Replace("\n", "<br/>");
                news.content = sb.ToString();
                news.content = ReplaceGP(news.content);
                news.ctime = Tools.GetDateTime(row["ob_rectime_0021"]);
                news.IsEnable = false;
                news.keyword = Tools.GetString(row["f013v_0021"]);
                news.ptime = null;
                news.Source = Tools.GetString(row["f003v_0021"]);
                news.step = 0;
                news.Summary = Tools.GetString(row["f007v_0021"]);
                news.title = Tools.GetString(row["f005v_0021"]);
                model.News.AddObject(news);
                model.SaveChanges();
                newsId = news.id;
                NewsImportId newImport = new NewsImportId();
                newImport.CpId = newId;
                newImport.NewsId = news.id;
                model.NewsImportId.AddObject(newImport);
                model.SaveChanges();
                //更新新闻和板块的关系
                ImportBk(news);
            }
            //if (ds.Tables[1].Rows.Count > 0)
            //{
            //    for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
            //    {
            //        DataRow row = ds.Tables[1].Rows[i];
            //        Attachment a = new Attachment();
            //        a.Content = (byte[])row["f005b_0025"];
            //        a.Desc = Tools.GetString(row["f003v_0025"]);
            //        a.EntryTime = Tools.GetDateTime(row["ob_rectime_0025"]);
            //        a.FileName = Tools.GetString(row["f002v_0025"]);
            //        a.Index = Tools.GetInt(row["f001n_0025"]);
            //        a.IsEnable = Tools.GetString(row["ob_isvalid_0025"]);
            //        a.NewsId = news.id;
            //        a.Pattern = Tools.GetString(row["f004v_0025"]);
            //        a.Remarks = Tools.GetString(row["ob_memo_0025"]);
            //        a.UpdateTime = Tools.GetDateTime(row["ob_modtime_0025"]);
            //        model.Attachment.AddObject(a);
            //    }
            //    model.SaveChanges();
            //}
        }
        else
        {
            newsId = model.NewsImportId.Where(x => x.CpId == newId).Select(x => x.NewsId).First().Value;
        }
        if (!model.News_Type.Any(x => x.NewsId == newsId && x.NewsTypeId == typeId))
        {
            News_Type t = new News_Type();
            t.NewsId = newsId;
            t.NewsTypeId = typeId;
            t.ctime = DateTime.Now;
            model.News_Type.AddObject(t);
            model.SaveChanges();
        }
        if (newsId > 0)
        {
            try
            {
                ImprtStock(newId, newsId);
            }
            catch
            {
            }
        }

     
    }

    public void ImportBk( News news)
    {
        CMSEntities dal = new CMSEntities();
        List<string> bkLst = dal.NewsReBK.Where(x => news.title.Contains(x.Key)).Select(x => x.BkId).ToList();
        foreach (var c in bkLst)
        {
            NewsIdReBkId tt = new NewsIdReBkId();
            tt.BkId = c;
            tt.CTime = DateTime.Now;
            tt.NewsId = news.id;
            if (!dal.NewsIdReBkId.Any(x => x.NewsId == news.id && x.BkId == c))
            {
                dal.NewsIdReBkId.AddObject(tt);
            }
        }
        dal.SaveChanges();
    }

    public StringBuilder ReplaceKG(StringBuilder sb)
    {
        return sb.Replace(" ", "&nbsp;").Replace("　", "&nbsp;&nbsp;");
    }

    public static string ReplaceGP(string str)
    {
        StringBuilder sb = new StringBuilder(str);
        foreach (string[] stock in CommonBll.CodeLst)
        {
            try
            {
                string sName = stock[0];
                string code = stock[1];
                string f = stock[2];
                if (string.IsNullOrEmpty(sName))
                {
                    continue;
                }
                string strCode = "<span ca=\"true\" t=\"" + f + "\" c=\"" + code + "\">" + code + "</span>";
                string strName = "<span ca=\"true\" t=\"" + f + "\" c=\"" + code + "\">" + sName + "</span>";
                sb = sb.Replace(code, strCode);
                sb = sb.Replace(sName, strName);
            }
            catch
            {

            }
        }
        return sb.ToString();
    }
}